package com.lanyou.esb.cook.proxy.redis.proxy.impl;

import java.io.IOException;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.cook.proxy.common.Constants4Redis;
import com.lanyou.esb.cook.proxy.redis.JedisPoolUtils;
import com.lanyou.esb.cook.proxy.redis.proxy.base.BaseListJedisProxy;

/**
 * 更新接口告警Redis操作代理 <br>
 * 
 * @author Davey.wu
 */
public class IfAlarmUpdateJedisProxy extends BaseListJedisProxy<String> {

	public IfAlarmUpdateJedisProxy() {
		this.setKey(Constants4Redis.REDIS_LIST_KEY_IF_ALARM_UPDATE);
	}

	@Override
	public void lpush(String key, String value) throws JedisException,
			JsonProcessingException {
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return;
		}
		boolean broken = false;
		try {
			jedis.lpush(key, value);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
	}

	@Override
	public void rpush(String key, String value) throws JedisException,
			JsonProcessingException {
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return;
		}
		boolean broken = false;
		try {
			jedis.rpush(key, value);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
	}

	@Override
	public String lpop(String key) throws JedisException, JsonParseException,
			JsonMappingException, IOException {
		String value = "";
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return "";
		}
		boolean broken = false;
		try {
			// 从左边获取一个元素
			value = jedis.lpop(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return value;
	}

}
